
package com.iplant.wms.controller.brm;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.BRMService;
import com.iplant.wms.service.po.brm.BRMSampleProp;
import com.iplant.wms.service.po.brm.BRMSampleRange;
import com.iplant.wms.service.po.brm.BRMSampleRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Sample")
public class SampleController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(SampleController.class);

	@Autowired
    BRMService wBRMService;

	@GetMapping("/PropAll")
	public Object PropAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			int wRuleID = StringUtils.parseInt(request.getParameter("RuleID"),-1);
			int wASNTypeID = StringUtils.parseInt(request.getParameter("ASNTypeID"),-1);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));
			int wActive =StringUtils.parseInt(request.getParameter("Active"),-1);

			Pagination wPagination = GetPagination(request);
			ServiceResult<List<BRMSampleProp>> wServiceResult = wBRMService.BRM_QuerySamplePropList( wLoginUser, wRuleID,
					wName,  wCode,wASNTypeID,
					 wActive,  wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/PropInfo")
	Object PropInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wASNTypeID = StringUtils.parseInt(request.getParameter("ASNTypeID"));
			double wMaterialNum = StringUtils.parseDouble(request.getParameter("MaterialNum"));
			String wMaterialNo = StringUtils.parseString(request.getParameter("MaterialNo"));

			ServiceResult<BRMSampleProp> wServiceResult = wBRMService.BRM_QuerySampleProp(wLoginUser, wMaterialNo, wASNTypeID, wMaterialNum);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServiceResult.Result);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}




	@PostMapping("/PropUpdate")
	public Object PropUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BRMSampleProp wBRMSampleProp = CloneTool.Clone(wParam.get("data"), BRMSampleProp.class);
			wBRMSampleProp.EditorName = wUserName;
			wBRMSampleProp.EditorID = wUserID;
			wBRMSampleProp.EditTime = Calendar.getInstance();
			if (wBRMSampleProp.ID <= 0) {

				wBRMSampleProp.CreatorName = wUserName;
				wBRMSampleProp.CreatorID = wUserID;
				wBRMSampleProp.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateSampleProp(wBMSEmployee, wBRMSampleProp);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSampleProp);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@PostMapping("/PropActive")
	public Object PropActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<BRMSampleProp> wBRMSamplePropList = CloneTool.CloneArray(wParam.get("data"), BRMSampleProp.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (BRMSampleProp wBRMSampleProp : wBRMSamplePropList) {

				wServiceResult = wBRMService.BRM_ActiveSampleProp(wBMSEmployee, wBRMSampleProp, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/RuleAll")
	Object RuleAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<BRMSampleRule>> wServiceResult = wBRMService.BRM_QuerySampleRuleList(wLoginUser,
					  wName, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/RuleInfo")
	Object RuleInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<BRMSampleRule> wServiceResult = wBRMService.BRM_QuerySampleRule(wLoginUser, wID, wCode);
			BRMSampleRule wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/RuleUpdate")
	Object RuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BRMSampleRule wBRMSampleRule = CloneTool.Clone(wParam.get("data"), BRMSampleRule.class);
			wBRMSampleRule.EditorName = wUserName;
			wBRMSampleRule.EditorID = wUserID;
			wBRMSampleRule.EditTime = Calendar.getInstance();
			if (wBRMSampleRule.ID <= 0) {

				wBRMSampleRule.CreatorName = wUserName;
				wBRMSampleRule.CreatorID = wUserID;
				wBRMSampleRule.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateSampleRule(wBMSEmployee, wBRMSampleRule);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSampleRule);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/RuleDelete")
	Object RuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BRMSampleRule wBRMSampleRule = CloneTool.Clone(wParam.get("data"), BRMSampleRule.class);

			ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteSampleRule(wBMSEmployee, wBRMSampleRule);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/RuleActive")
	Object RuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<BRMSampleRule> wBRMSampleRuleList = CloneTool.CloneArray(wParam.get("data"), BRMSampleRule.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (BRMSampleRule wBRMSampleRule : wBRMSampleRuleList) {

				wServiceResult = wBRMService.BRM_ActiveSampleRule(wBMSEmployee, wBRMSampleRule, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	
	

	@GetMapping("/RangeAll")
	Object RangeAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"),-1);
			int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"),-1);
			String wMaterialLike = StringUtils.parseString(request.getParameter("MaterialLike"));
			String wMaterialCode = StringUtils.parseString(request.getParameter("MaterialCode"));

			String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));
			Pagination wPagination = GetPagination(request);

			ServiceResult<List<BRMSampleRange>> wServiceResult = wBRMService.BRM_QuerySampleRangeList(
					wLoginUser, wMaterialGroupID, wMaterialGroupFullName,wMaterialID, wMaterialCode, wMaterialLike, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@PostMapping("/RangeUpdate")
	Object RangeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BRMSampleRange wBRMSampleRange = CloneTool.Clone(wParam.get("data"), BRMSampleRange.class);

			wBRMSampleRange.setUserInfo(wBMSEmployee);

			ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateSampleRange(wBMSEmployee, wBRMSampleRange);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSampleRange);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


	@PostMapping("/RangeDelete")
	Object RangeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			BRMSampleRange wBRMSampleRange = CloneTool.Clone(wParam.get("data"), BRMSampleRange.class);

			wBRMSampleRange.setUserInfo(wBMSEmployee);

			ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteSampleRange(wBMSEmployee, wBRMSampleRange);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSampleRange);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}





}
